package pl.edu.pk.cc.client;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;

import pl.edu.pk.cc.client.connections.CCServletCalls;
import pl.edu.pk.cc.client.connections.CCSimpleServerCaller;
import pl.edu.pk.cc.client.worker.CCWorkerContent;
import pl.edu.pk.cc.common.dto.CCLoginDataDTO;
import pl.edu.pk.cc.common.dto.CCWorkerDTO;
import pl.edu.pk.cc.common.util.CCPermissions;
import pl.edu.pk.cc.server.hibernate.CCHibernateUtil;

/**
 * Servlet implementation class CCIndex
 */
public class CCIndex extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CCPage page;
	String pageName;
	HttpSession sesja;
	PrintWriter out;
	boolean log = false;
	String userlaw = "niezalogowany";
	public static CCWorkerDTO worker;
	public static CCLoginDataDTO login_data;
	static int sukces = 0;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CCIndex() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		sesja = request.getSession();

		response.setContentType("text/html;charset=UTF-8");
		out = response.getWriter();
		page = new CCPage();
		pageName = request.getParameter("strona");
		if (pageName == null) {
			pageName = "";
		}
		String logged, login_panel;

		if (sesja.getAttribute("user") != null) {
			logged = (String) sesja.getAttribute("user");
			login_panel = page.wyloguj();
			log = true;
			userlaw = (String) sesja.getAttribute("law");
		} else {
			logged = "niezalogowany";
			login_panel = page.logowanie();
			log = false;
		}

		if (sesja.getAttribute("success") != null) {
			sukces = (Integer) sesja.getAttribute("success");
			sesja.setAttribute("success", 0);
		} else
			sukces = 0;

		out.println(page.poczatekHTML() + page.header("Call Center Booster", logged) + page.newsy() + page.menu(userlaw) + login_panel + page.tresc(pageName, sukces, userlaw, sesja) + page.stopka()
				+ page.koniecHTML());

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		sesja = request.getSession();
		String spr = request.getParameter("zal");

		if (spr != null && spr.equals("zal")) {
			String login = request.getParameter("log");
			String password = request.getParameter("pass");
			if (!(login.equals("") || (password.equals("")))) {

				if (CCSimpleServerCaller.login(login, password, "http://localhost:8080/ISI-CallCenter/CCMainServlet")) {
					worker = CCMain.getCurrentWorker();
					Session session = CCHibernateUtil.getSessionFactory().getCurrentSession();
					session.beginTransaction();
					session.refresh(worker);
					login_data = worker.getLogin();

					CCPermissions perm = login_data.getPermissions();
					switch (perm) {
					case USER:
						sesja.setAttribute("user", login);
						log = true;
						userlaw = "user";
						sesja.setAttribute("workerContent", CCWorkerContent.createWorkerContent());
						break;
					case ADMIN:
						sesja.setAttribute("user", login);
						log = true;
						userlaw = "admin";
						break;
					case CHIEF:
						sesja.setAttribute("user", login);
						userlaw = "chief";
						log = true;
						break;
					}
					sesja.setAttribute("law", userlaw);
					session.close();
				}

			}
			// else {
			// sesja.setAttribute("user", "niezalogowany");
			// sesja.setAttribute("workerContent", null);
			// }
		} else if (sesja.getAttribute("workerContent") != null) {
			((CCWorkerContent) sesja.getAttribute("workerContent")).proceedAction(request);
			if (spr != null && spr.equals("wyl")) {
				CCSimpleServerCaller.logout();
				sesja.invalidate();
				log = false;
				userlaw = "niezalogowany";

			}
		} else if (spr != null && spr.equals("wyl")) {
			sesja.invalidate();
			log = false;
			userlaw = "niezalogowany";

		}

		doGet(request, response);
	}

	@Override
	public void init() throws ServletException {
		super.init();
		CCServletCalls.init("http://localhost:8080/ISI-CallCenter/CCMainServlet");

	}

	public static void setSuccess(int value) {
		sukces = value;
	}
	// TODO Odkmoentowac jak projekt bedzie gotowy
	/*
	 * @Override public void destroy() { super.destroy(); sesja.invalidate(); System.gc(); }
	 */

}
